/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Android version
 Copyright (C) 2003-11  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/app/NotificationMan.java
 Version: 2  Date: 23.07.2011
 Author(s): Ashikhmin Kirill & LSDsl
 *******************************************************************************/

package org.jimm.app;

import java.util.ArrayList;
import java.util.HashMap;

import org.jimm.app.util.Func;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class NotificationMan {
	

	
    private static HashMap<String, Integer> cont2notifId = new HashMap<String, Integer>();
    private static HashMap<String, Integer> cont2notifCol = new HashMap<String, Integer>();
    private static int ids = 10;
    private static android.app.NotificationManager mNotificationManager;

    private static ArrayList<String> skipList = new ArrayList<String>();

    
    /**
	 * Show notify for contact
	 * 
	 * @param context
	 *            - context service
	 * @param contactName
	 *            - Name contacts
	 * @param contactId
	 *            - ID contact
	 * @param msgText
	 *            - text message
	 * @param AccLogin
	 *            - Account login
	 * @param AccTypeText
	 *            - Type of account
	 *       
	 */
    public static void addNotification(Context context, String contactName, String contactId, String msgText, String AccLogin, String AccTypeText) {

        if (skipList.indexOf(contactId) == -1) {
            Intent intent = new Intent(context, ChatActivity.class);
            intent.putExtra(ChatActivity.CONTACT_ID_EXTRA, contactId);
            intent.putExtra(ChatActivity.ACCOUNT_ID_EXTRA, AccLogin);
            mNotificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            int icon = R.drawable.message;
            CharSequence tickerText = contactName + ":" + msgText;
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);
            
            Integer col = cont2notifCol.get(contactId);
            if (col == null) {col = 1;} else {col = col+1;}
            cont2notifCol.put(contactId, col);

            String wordEnd=Func.WordEnd(col,context);           
            CharSequence contentTitle = contactName + "(" + AccLogin + "-" + AccTypeText + ")"+" : "+col+" "+wordEnd;
            CharSequence contentText = msgText;
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            Integer id = cont2notifId.get(contactId); 
            if (id == null) {
                id = ids++;
                cont2notifId.put(contactId, id);
            }           
            if (col>1) notification.number = col;
            mNotificationManager.cancel(id);
            mNotificationManager.notify(id, notification); // show notify
        }
    }

    /**
	 * clear notify for contact
	 * 
	 * @param contactId
	 *            - Id contact
	 */
	public static void clearNotification(String contactName) {
        final Integer id = cont2notifId.get(contactName);
        if (id != null) mNotificationManager.cancel(id);
    }

	
	/**
	 * skip show notify for contact
	 * 
	 * @param contactId
	 *            - Id contact
	 */
    public static void skipNotifyFrom(String contactId) {
        skipList.add(contactId);
    }
    
    /**
	 * restore show notify for contact
	 * 
	 * @param contactId
	 *            - Id contact
	 */
    public static void revertNotifyFrom(String contactId) {
        skipList.remove(contactId);
        cont2notifCol.put(contactId, 0);
    }
}
